<?php
namespace Home\Controller;
use Think\Controller;
class ShoppingController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['user_is_login']){
			$this->redirect('home/User/login');
		}
	}
	public function addTOCart(){
		$user_id=$_SESSION['user_id'];
		$product_id=I('post.product_id');
		$num=I('post.num');
		$data=array(
			'user_id'=>$user_id,
			'product_id'=>$product_id,
			'num'=>$num
		);
		$query=M('user_cart')->where(array('user_id'=>$user_id,'product_id'=>$product_id))->find();
		if($query){
			$query['num'] += $num;
			M('user_cart')->where(array('user_id'=>$user_id,'product_id'=>$product_id))->save($query); // 根据条件更新记录
		}else{
			M('user_cart')->add($data);
		}
		$this->success('成功添加购物车',U('home/product/detail/id/'.$product_id));
	}
    public function shopcart(){
    	$user_cata=M('user_cart')->where(array('user_id'=>$_SESSION['user_id']))->select();
    	$this->assign('user_cata',$user_cata);
		$this->display();
    }
    public function shopcart_delete($id){
   		M('user_cart')->where('id='.$id)->delete();
   		$this->redirect('home/Shopping/shopcart');
   	}
   	public function cartdels(){
    	$ids=I('post.ids');
    	M('user_cart')->where('id in('.implode(',',$ids).')')->delete();
    	echo 'ok';
    }
    public function update_cart_num(){
    	$id=I('get.id');
    	$num=I('get.num');
    	if($num>0){
    		M('user_cart')->where('id='.$id)->save(array('num'=>$num));
    		echo 'ok';
    	}else{
    		echo '至少为一件商品！';
    	}
    }
   	public function createorder(){
   		if(!I('post.data')){
   			echo '请选择一件商品';
   		}else{
   			$_SESSION['data']=I('post.data');
   			echo 'ok';
   		}
   	}
   	public function shoporder(){
   		$address=M('user_address')->where(array('user_id'=>$_SESSION['user_id']))->select();
   		$data=$_SESSION['data'];
   		$this->assign('address',$address);
   		$this->assign('data',$data);
   		$this->display();
   	}
   	public function orderProduct(){
   		$address_id=I('post.address_id');
  		$product_id=I('post.product_id');
      $user=M('user_admin')->where('id='.$_SESSION['user_id'])->find();
      $address=M('user_address')->where('id='.$address_id)->find();
  		$num=I('post.num');
      
  		$order=M('orders')->add(array(
  			'user_id'=>$_SESSION['user_id'],
  			'address_id'=>$address_id,
  			'create_time'=>time(),
        'user_name'=>$user['username'],
        'address_name'=>$address['name'],
        'address_address'=>$address['address'],
        'address_phone'=>$address['phone'],
  		));
      $count=count($product_id);
      $total=0;
      for($i=0;$i<$count;$i++){
        $product=M('product')->where('id='.$product_id[$i])->find();
        $total+=floatval($product['price'])*intval($num[$i]);
        M('order_products')->add(array(
          'product_id'=>$product_id[$i],
          'order_id'=>$order['id'],
          'num'=>$num[$i],
          'product_title'=>$product['title'],
          'product_catalog_id'=>$product['catalog_id'],
          'product_price'=>$product['price'],
          'total'=>$total
        ));
      }
      M('orders')->where('id='.$order['id'])->save(array('total'=>$total));
		  //$this->redirect('shoporder');
      $this->success('结算成功',U('home/Front/index'));
   	}
   	public function add_one_ADDress(){
   		$name=I('post.name');
   		$address=I('post.address');
   		$phone=I('post.phone');
   		if(empty($name)){
          echo json_encode(array('code' => 'error','filed' => 'name','msg' => "姓名不能为空"));
          exit();
      }
       if(empty($address)){
          echo json_encode(array('code' => 'error','filed' => 'address','msg' => "地址不能为空"));
          exit();
      }
       if(empty($phone)){
          echo json_encode(array('code' => 'error','filed' => 'phone','msg' => "电话不能为空"));
          exit();
      }
   		M('user_address')->add(array(
   			'user_id'=>$_SESSION['user_id'],
   			'name'=>$name,
   			'address'=>$address,
   			'phone'=>$phone
   		));
      //echo 'ok';
   		echo json_encode(array('code' => 'ok'));
   	}
   	public function del_one_address($id){
   		M('user_address')->where('id='.$id)->delete();
   		$this->redirect('shoporder');
   	}
}